package com.jiangjiang.eshop.wms.service.impl;

import com.jiangjiang.eshop.purchase.service.PurchaseFacadeService;
import com.jiangjiang.eshop.purchase.service.PurchaseOrderService;
import com.jiangjiang.eshop.wms.constant.PurchaseInputOrderStatus;
import com.jiangjiang.eshop.wms.constant.WmsStockUpdateEvent;
import com.jiangjiang.eshop.wms.domain.PurchaseInputOrderEntity;
import com.jiangjiang.eshop.wms.dto.PurchaseInputOrderDTO;
import com.jiangjiang.eshop.wms.dto.ReturnGoodsInputOrderDTO;
import com.jiangjiang.eshop.wms.dto.SaleDeliveryOrderDTO;
import com.jiangjiang.eshop.schedule.dto.SaleDeliveryScheduleResult;
import com.jiangjiang.eshop.wms.service.PurchaseInputOrderService;
import com.jiangjiang.eshop.wms.service.ReturnGoodsInputOrderService;
import com.jiangjiang.eshop.wms.service.SaleDeliveryOrderService;
import com.jiangjiang.eshop.wms.service.WmsFacadeService;
import com.jiangjiang.eshop.wms.stock.WmsStockUpdater;
import com.jiangjiang.eshop.wms.stock.WmsStockUpdaterFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName WmsFacadeServiceImpl
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/24 19:34
 * @Version 1.0
 **/
@Slf4j
@Service
public class WmsFacadeServiceImpl implements WmsFacadeService {

    @Autowired
    private SaleDeliveryOrderService saleDeliveryOrderService;
    @Autowired
    private WmsStockUpdaterFactory wmsStockUpdaterFactory;
    @Autowired
    private PurchaseInputOrderService purchaseInputOrderService;
    @Autowired
    private ReturnGoodsInputOrderService returnGoodsInputOrderService;
    @Autowired
    private PurchaseFacadeService purchaseFacadeService;


    @Override
    public Boolean createPurchaseInputOrder(PurchaseInputOrderDTO purchaseInputOrderDTO) {
        try {
            return purchaseInputOrderService.save(purchaseInputOrderDTO);
        } catch (Exception e) {
            log.warn("WmsFacadeServiceImpl createPurchaseInputOrder error", e);
        }
        return false;
    }

    @Override
    public Boolean createSaleDeliveryOrder(SaleDeliveryOrderDTO saleDeliveryOrderDTO) {
        try {
            return saleDeliveryOrderService.saveSaleDeliveryOrder(saleDeliveryOrderDTO);
        } catch (Exception e) {
            log.warn("WmsFacadeServiceImpl createSaleDeliveryOrder error", e);
        }
        return false;
    }

    @Override
    public Boolean createReturnGoodsInputOrder(ReturnGoodsInputOrderDTO returnGoodsInputOrderDTO) {
        try {
            return returnGoodsInputOrderService.saveReturnGoodsInputOrder(returnGoodsInputOrderDTO);
        } catch (Exception e) {
            log.warn("WmsFacadeServiceImpl createReturnGoodsInputOrder error", e);
        }
        return false;
    }


    @Override
    public Boolean informSubmitOrderEvent(SaleDeliveryScheduleResult scheduleResult) {
        try {
            WmsStockUpdater wmsStockUpdater = wmsStockUpdaterFactory.create(WmsStockUpdateEvent.SUBMIT_ORDER, scheduleResult);
            return wmsStockUpdater.update();
        } catch (Exception e) {
            log.warn("WmsFacadeServiceImpl informSubmitOrderEvent error", e);
        }
        return false;
    }

    @Override
    public Boolean informPayOrderEvent(SaleDeliveryScheduleResult scheduleResult) {
        try {
            WmsStockUpdater wmsStockUpdater = wmsStockUpdaterFactory.create(WmsStockUpdateEvent.PAY_ORDER, scheduleResult);
            return wmsStockUpdater.update();
        } catch (Exception e) {
            log.warn("WmsFacadeServiceImpl informPayOrderEvent error", e);
        }
        return false;
    }

    @Override
    public Boolean informCancelOrderEvent(SaleDeliveryScheduleResult scheduleResult) {
        try {
            WmsStockUpdater wmsStockUpdater = wmsStockUpdaterFactory.create(WmsStockUpdateEvent.CANCEL_ORDER, scheduleResult);
            return wmsStockUpdater.update();
        } catch (Exception e) {
            log.warn("WmsFacadeServiceImpl informPayOrderEvent error", e);
        }
        return false;
    }

    @Override
    public String getLogisticCode(Long orderId) {

        try {
            SaleDeliveryOrderDTO saleDeliveryOrderDTO = saleDeliveryOrderService.queryByOrderId(orderId);
            return saleDeliveryOrderDTO.getLogisticOrderDTO().getLogisticCode();
        } catch (Exception e) {
            log.warn("WmsFacadeServiceImpl getLogisticCode error", e);
        }
        return null;
    }

    @Override
    public Boolean informCreatePurchaseSettlementOrderEvent(Long purchaseInputOrderId) {
        try {
            purchaseInputOrderService.updateStatus(purchaseInputOrderId, PurchaseInputOrderStatus.WAIT_FOR_SETTLEMENT);
            PurchaseInputOrderDTO dto = purchaseInputOrderService.queryById(purchaseInputOrderId);
            return purchaseFacadeService.informCreatePurchaseSettlementOrderEvent(dto.getPurchaseOrderId());
        } catch (Exception e) {
            log.warn("WmsFacadeServiceImpl informCreatePurchaseSettlementOrderEvent error", e);
        }
        return false;
    }

    @Override
    public Boolean informFinishedPurchaseSettlementOrderEvent(Long purchaseInputOrderId) {
        try {
            purchaseInputOrderService.updateStatus(purchaseInputOrderId, PurchaseInputOrderStatus.FINISHED);
            PurchaseInputOrderDTO dto = purchaseInputOrderService.queryById(purchaseInputOrderId);
            return purchaseFacadeService.informFinishedPurchaseSettleOrderEvent(dto.getPurchaseOrderId());
        } catch (Exception e) {
            log.warn("WmsFacadeServiceImpl informFinishedPurchaseSettlementOrderEvent error", e);
        }
        return false;
    }
}
